<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>promise_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_promise_promise_test.js.html">promise_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.PromiseTest&#39;);
<a name="line16"></a>
<a name="line17"></a>goog.require(&#39;goog.Promise&#39;);
<a name="line18"></a>goog.require(&#39;goog.Thenable&#39;);
<a name="line19"></a>goog.require(&#39;goog.functions&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.AsyncTestCase&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.setTestOnly(&#39;goog.PromiseTest&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>// TODO(brenneman):
<a name="line30"></a>// - Add tests for interoperability with native Promises where available.
<a name="line31"></a>// - Make most tests use the MockClock (though some tests should still verify
<a name="line32"></a>//   real asynchronous behavior.
<a name="line33"></a>// - Add tests for long stack traces.
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>var mockClock;
<a name="line37"></a>var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall(document.title);
<a name="line38"></a>var stubs = new goog.testing.PropertyReplacer();
<a name="line39"></a>var unhandledRejections;
<a name="line40"></a>
<a name="line41"></a>
<a name="line42"></a>// Simple shared objects used as test values.
<a name="line43"></a>var dummy = {toString: goog.functions.constant(&#39;[object dummy]&#39;)};
<a name="line44"></a>var sentinel = {toString: goog.functions.constant(&#39;[object sentinel]&#39;)};
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>function setUpPage() {
<a name="line48"></a>  asyncTestCase.stepTimeout = 200;
<a name="line49"></a>  mockClock = new goog.testing.MockClock();
<a name="line50"></a>}
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>function setUp() {
<a name="line54"></a>  unhandledRejections = goog.testing.recordFunction();
<a name="line55"></a>  goog.Promise.setUnhandledRejectionHandler(unhandledRejections);
<a name="line56"></a>}
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>function tearDown() {
<a name="line60"></a>  if (mockClock) {
<a name="line61"></a>    // The system should leave no pending unhandled rejections. Advance the mock
<a name="line62"></a>    // clock to the end of time to catch any rethrows waiting in the queue.
<a name="line63"></a>    mockClock.tick(Infinity);
<a name="line64"></a>    mockClock.uninstall();
<a name="line65"></a>    mockClock.reset();
<a name="line66"></a>  }
<a name="line67"></a>  stubs.reset();
<a name="line68"></a>}
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>function tearDownPage() {
<a name="line72"></a>  goog.dispose(mockClock);
<a name="line73"></a>}
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>function continueTesting() {
<a name="line77"></a>  asyncTestCase.continueTesting();
<a name="line78"></a>}
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Dummy onfulfilled or onrejected function that should not be called.
<a name="line83"></a> *
<a name="line84"></a> * @param {*} result The result passed into the callback.
<a name="line85"></a> */
<a name="line86"></a>function shouldNotCall(result) {
<a name="line87"></a>  fail(&#39;This should not have been called (result: &#39; + String(result) + &#39;)&#39;);
<a name="line88"></a>}
<a name="line89"></a>
<a name="line90"></a>
<a name="line91"></a>function fulfillSoon(value, delay) {
<a name="line92"></a>  return new goog.Promise(function(resolve, reject) {
<a name="line93"></a>    window.setTimeout(function() {
<a name="line94"></a>      resolve(value);
<a name="line95"></a>    }, delay);
<a name="line96"></a>  });
<a name="line97"></a>}
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>function rejectSoon(reason, delay) {
<a name="line101"></a>  return new goog.Promise(function(resolve, reject) {
<a name="line102"></a>    window.setTimeout(function() {
<a name="line103"></a>      reject(reason);
<a name="line104"></a>    }, delay);
<a name="line105"></a>  });
<a name="line106"></a>}
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>function testThenIsFulfilled() {
<a name="line110"></a>  asyncTestCase.waitForAsync();
<a name="line111"></a>  var timesCalled = 0;
<a name="line112"></a>
<a name="line113"></a>  var p = new goog.Promise(function(resolve, reject) {
<a name="line114"></a>    resolve(sentinel);
<a name="line115"></a>  });
<a name="line116"></a>  p.then(function(value) {
<a name="line117"></a>    timesCalled++;
<a name="line118"></a>    assertEquals(sentinel, value);
<a name="line119"></a>    assertEquals(&#39;onFulfilled must be called exactly once.&#39;, 1, timesCalled);
<a name="line120"></a>  });
<a name="line121"></a>  p.thenAlways(continueTesting);
<a name="line122"></a>
<a name="line123"></a>  assertEquals(&#39;then() must return before callbacks are invoked.&#39;,
<a name="line124"></a>               0, timesCalled);
<a name="line125"></a>}
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>function testThenIsRejected() {
<a name="line129"></a>  asyncTestCase.waitForAsync();
<a name="line130"></a>  var timesCalled = 0;
<a name="line131"></a>
<a name="line132"></a>  var p = new goog.Promise(function(resolve, reject) {
<a name="line133"></a>    reject(sentinel);
<a name="line134"></a>  });
<a name="line135"></a>  p.then(shouldNotCall, function(value) {
<a name="line136"></a>    timesCalled++;
<a name="line137"></a>    assertEquals(sentinel, value);
<a name="line138"></a>    assertEquals(&#39;onRejected must be called exactly once.&#39;, 1, timesCalled);
<a name="line139"></a>  });
<a name="line140"></a>  p.thenAlways(continueTesting);
<a name="line141"></a>
<a name="line142"></a>  assertEquals(&#39;then() must return before callbacks are invoked.&#39;,
<a name="line143"></a>               0, timesCalled);
<a name="line144"></a>}
<a name="line145"></a>
<a name="line146"></a>
<a name="line147"></a>function testOptionalOnFulfilled() {
<a name="line148"></a>  asyncTestCase.waitForAsync();
<a name="line149"></a>
<a name="line150"></a>  goog.Promise.resolve(sentinel).
<a name="line151"></a>      then(null, null).
<a name="line152"></a>      then(null, shouldNotCall).
<a name="line153"></a>      then(function(value) {
<a name="line154"></a>        assertEquals(sentinel, value);
<a name="line155"></a>      }).
<a name="line156"></a>      thenAlways(continueTesting);
<a name="line157"></a>}
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>function testOptionalOnRejected() {
<a name="line161"></a>  asyncTestCase.waitForAsync();
<a name="line162"></a>
<a name="line163"></a>  goog.Promise.reject(sentinel).
<a name="line164"></a>      then(null, null).
<a name="line165"></a>      then(shouldNotCall).
<a name="line166"></a>      then(null, function(reason) {
<a name="line167"></a>        assertEquals(sentinel, reason);
<a name="line168"></a>      }).
<a name="line169"></a>      thenAlways(continueTesting);
<a name="line170"></a>}
<a name="line171"></a>
<a name="line172"></a>
<a name="line173"></a>function testMultipleResolves() {
<a name="line174"></a>  asyncTestCase.waitForAsync();
<a name="line175"></a>  var timesCalled = 0;
<a name="line176"></a>  var resolvePromise;
<a name="line177"></a>
<a name="line178"></a>  var p = new goog.Promise(function(resolve, reject) {
<a name="line179"></a>    resolvePromise = resolve;
<a name="line180"></a>    resolve(&#39;foo&#39;);
<a name="line181"></a>    resolve(&#39;bar&#39;);
<a name="line182"></a>  });
<a name="line183"></a>
<a name="line184"></a>  p.then(function(value) {
<a name="line185"></a>    timesCalled++;
<a name="line186"></a>    assertEquals(&#39;onFulfilled must be called exactly once.&#39;, 1, timesCalled);
<a name="line187"></a>  });
<a name="line188"></a>
<a name="line189"></a>  // Add one more test for fulfilling after a delay.
<a name="line190"></a>  window.setTimeout(function() {
<a name="line191"></a>    resolvePromise(&#39;baz&#39;);
<a name="line192"></a>    assertEquals(1, timesCalled);
<a name="line193"></a>    continueTesting();
<a name="line194"></a>  }, 10);
<a name="line195"></a>}
<a name="line196"></a>
<a name="line197"></a>
<a name="line198"></a>function testMultipleRejects() {
<a name="line199"></a>  asyncTestCase.waitForAsync();
<a name="line200"></a>  var timesCalled = 0;
<a name="line201"></a>  var rejectPromise;
<a name="line202"></a>
<a name="line203"></a>  var p = new goog.Promise(function(resolve, reject) {
<a name="line204"></a>    rejectPromise = reject;
<a name="line205"></a>    reject(&#39;foo&#39;);
<a name="line206"></a>    reject(&#39;bar&#39;);
<a name="line207"></a>  });
<a name="line208"></a>
<a name="line209"></a>  p.then(shouldNotCall, function(value) {
<a name="line210"></a>    timesCalled++;
<a name="line211"></a>    assertEquals(&#39;onRejected must be called exactly once.&#39;, 1, timesCalled);
<a name="line212"></a>  });
<a name="line213"></a>
<a name="line214"></a>  // Add one more test for rejecting after a delay.
<a name="line215"></a>  window.setTimeout(function() {
<a name="line216"></a>    rejectPromise(&#39;baz&#39;);
<a name="line217"></a>    assertEquals(1, timesCalled);
<a name="line218"></a>    continueTesting();
<a name="line219"></a>  }, 10);
<a name="line220"></a>}
<a name="line221"></a>
<a name="line222"></a>
<a name="line223"></a>function testAsynchronousThenCalls() {
<a name="line224"></a>  asyncTestCase.waitForAsync();
<a name="line225"></a>  var timesCalled = [0, 0, 0, 0];
<a name="line226"></a>  var p = new goog.Promise(function(resolve, reject) {
<a name="line227"></a>    window.setTimeout(function() {
<a name="line228"></a>      resolve();
<a name="line229"></a>    }, 30);
<a name="line230"></a>  });
<a name="line231"></a>
<a name="line232"></a>  p.then(function() {
<a name="line233"></a>    timesCalled[0]++;
<a name="line234"></a>    assertArrayEquals([1, 0, 0, 0], timesCalled);
<a name="line235"></a>  });
<a name="line236"></a>
<a name="line237"></a>  window.setTimeout(function() {
<a name="line238"></a>    p.then(function() {
<a name="line239"></a>      timesCalled[1]++;
<a name="line240"></a>      assertArrayEquals([1, 1, 0, 0], timesCalled);
<a name="line241"></a>    });
<a name="line242"></a>  }, 10);
<a name="line243"></a>
<a name="line244"></a>  window.setTimeout(function() {
<a name="line245"></a>    p.then(function() {
<a name="line246"></a>      timesCalled[2]++;
<a name="line247"></a>      assertArrayEquals([1, 1, 1, 0], timesCalled);
<a name="line248"></a>    });
<a name="line249"></a>  }, 20);
<a name="line250"></a>
<a name="line251"></a>  window.setTimeout(function() {
<a name="line252"></a>    p.then(function() {
<a name="line253"></a>      timesCalled[3]++;
<a name="line254"></a>      assertArrayEquals([1, 1, 1, 1], timesCalled);
<a name="line255"></a>    });
<a name="line256"></a>    p.thenAlways(continueTesting);
<a name="line257"></a>  }, 40);
<a name="line258"></a>}
<a name="line259"></a>
<a name="line260"></a>
<a name="line261"></a>function testResolveWithPromise() {
<a name="line262"></a>  asyncTestCase.waitForAsync();
<a name="line263"></a>  var resolveBlocker;
<a name="line264"></a>  var hasFulfilled = false;
<a name="line265"></a>  var blocker = new goog.Promise(function(resolve, reject) {
<a name="line266"></a>    resolveBlocker = resolve;
<a name="line267"></a>  });
<a name="line268"></a>
<a name="line269"></a>  var p = goog.Promise.resolve(blocker);
<a name="line270"></a>  p.then(function(value) {
<a name="line271"></a>    hasFulfilled = true;
<a name="line272"></a>    assertEquals(sentinel, value);
<a name="line273"></a>  }, shouldNotCall);
<a name="line274"></a>  p.thenAlways(function() {
<a name="line275"></a>    assertTrue(hasFulfilled);
<a name="line276"></a>    continueTesting();
<a name="line277"></a>  });
<a name="line278"></a>
<a name="line279"></a>  assertFalse(hasFulfilled);
<a name="line280"></a>  resolveBlocker(sentinel);
<a name="line281"></a>}
<a name="line282"></a>
<a name="line283"></a>
<a name="line284"></a>function testResolveWithRejectedPromise() {
<a name="line285"></a>  asyncTestCase.waitForAsync();
<a name="line286"></a>  var rejectBlocker;
<a name="line287"></a>  var hasRejected = false;
<a name="line288"></a>  var blocker = new goog.Promise(function(resolve, reject) {
<a name="line289"></a>    rejectBlocker = reject;
<a name="line290"></a>  });
<a name="line291"></a>
<a name="line292"></a>  var p = goog.Promise.resolve(blocker);
<a name="line293"></a>  p.then(shouldNotCall, function(reason) {
<a name="line294"></a>    hasRejected = true;
<a name="line295"></a>    assertEquals(sentinel, reason);
<a name="line296"></a>  });
<a name="line297"></a>  p.thenAlways(function() {
<a name="line298"></a>    assertTrue(hasRejected);
<a name="line299"></a>    continueTesting();
<a name="line300"></a>  });
<a name="line301"></a>
<a name="line302"></a>  assertFalse(hasRejected);
<a name="line303"></a>  rejectBlocker(sentinel);
<a name="line304"></a>}
<a name="line305"></a>
<a name="line306"></a>
<a name="line307"></a>function testRejectWithPromise() {
<a name="line308"></a>  asyncTestCase.waitForAsync();
<a name="line309"></a>  var resolveBlocker;
<a name="line310"></a>  var hasFulfilled = false;
<a name="line311"></a>  var blocker = new goog.Promise(function(resolve, reject) {
<a name="line312"></a>    resolveBlocker = resolve;
<a name="line313"></a>  });
<a name="line314"></a>
<a name="line315"></a>  var p = goog.Promise.reject(blocker);
<a name="line316"></a>  p.then(function(value) {
<a name="line317"></a>    hasFulfilled = true;
<a name="line318"></a>    assertEquals(sentinel, value);
<a name="line319"></a>  }, shouldNotCall);
<a name="line320"></a>  p.thenAlways(function() {
<a name="line321"></a>    assertTrue(hasFulfilled);
<a name="line322"></a>    continueTesting();
<a name="line323"></a>  });
<a name="line324"></a>
<a name="line325"></a>  assertFalse(hasFulfilled);
<a name="line326"></a>  resolveBlocker(sentinel);
<a name="line327"></a>}
<a name="line328"></a>
<a name="line329"></a>
<a name="line330"></a>function testRejectWithRejectedPromise() {
<a name="line331"></a>  asyncTestCase.waitForAsync();
<a name="line332"></a>  var rejectBlocker;
<a name="line333"></a>  var hasRejected = false;
<a name="line334"></a>  var blocker = new goog.Promise(function(resolve, reject) {
<a name="line335"></a>    rejectBlocker = reject;
<a name="line336"></a>  });
<a name="line337"></a>
<a name="line338"></a>  var p = goog.Promise.reject(blocker);
<a name="line339"></a>  p.then(shouldNotCall, function(reason) {
<a name="line340"></a>    hasRejected = true;
<a name="line341"></a>    assertEquals(sentinel, reason);
<a name="line342"></a>  });
<a name="line343"></a>  p.thenAlways(function() {
<a name="line344"></a>    assertTrue(hasRejected);
<a name="line345"></a>    continueTesting();
<a name="line346"></a>  });
<a name="line347"></a>
<a name="line348"></a>  assertFalse(hasRejected);
<a name="line349"></a>  rejectBlocker(sentinel);
<a name="line350"></a>}
<a name="line351"></a>
<a name="line352"></a>
<a name="line353"></a>function testResolveAndReject() {
<a name="line354"></a>  asyncTestCase.waitForAsync();
<a name="line355"></a>  var onFulfilledCalled = false;
<a name="line356"></a>  var onRejectedCalled = false;
<a name="line357"></a>  var p = new goog.Promise(function(resolve, reject) {
<a name="line358"></a>    resolve();
<a name="line359"></a>    reject();
<a name="line360"></a>  });
<a name="line361"></a>
<a name="line362"></a>  p.then(function() {
<a name="line363"></a>    onFulfilledCalled = true;
<a name="line364"></a>  }, function() {
<a name="line365"></a>    onRejectedCalled = true;
<a name="line366"></a>  });
<a name="line367"></a>
<a name="line368"></a>  p.thenAlways(function() {
<a name="line369"></a>    assertTrue(onFulfilledCalled);
<a name="line370"></a>    assertFalse(onRejectedCalled);
<a name="line371"></a>    continueTesting();
<a name="line372"></a>  });
<a name="line373"></a>}
<a name="line374"></a>
<a name="line375"></a>
<a name="line376"></a>function testRejectAndResolve() {
<a name="line377"></a>  asyncTestCase.waitForAsync();
<a name="line378"></a>  var onFulfilledCalled = false;
<a name="line379"></a>  var onRejectedCalled = false;
<a name="line380"></a>  var p = new goog.Promise(function(resolve, reject) {
<a name="line381"></a>    reject();
<a name="line382"></a>    resolve();
<a name="line383"></a>  });
<a name="line384"></a>
<a name="line385"></a>  p.then(function() {
<a name="line386"></a>    onFulfilledCalled = true;
<a name="line387"></a>  }, function() {
<a name="line388"></a>    onRejectedCalled = true;
<a name="line389"></a>  });
<a name="line390"></a>
<a name="line391"></a>  p.thenAlways(function() {
<a name="line392"></a>    assertTrue(onRejectedCalled);
<a name="line393"></a>    assertFalse(onFulfilledCalled);
<a name="line394"></a>    continueTesting();
<a name="line395"></a>  });
<a name="line396"></a>}
<a name="line397"></a>
<a name="line398"></a>
<a name="line399"></a>function testThenReturnsBeforeCallbackWithFulfill() {
<a name="line400"></a>  asyncTestCase.waitForAsync();
<a name="line401"></a>  var thenHasReturned = false;
<a name="line402"></a>  var p = goog.Promise.resolve();
<a name="line403"></a>
<a name="line404"></a>  p.then(function() {
<a name="line405"></a>    assertTrue(
<a name="line406"></a>        &#39;Callback must be called only after then() has returned.&#39;,
<a name="line407"></a>        thenHasReturned);
<a name="line408"></a>  });
<a name="line409"></a>  p.thenAlways(continueTesting);
<a name="line410"></a>  thenHasReturned = true;
<a name="line411"></a>}
<a name="line412"></a>
<a name="line413"></a>
<a name="line414"></a>function testThenReturnsBeforeCallbackWithReject() {
<a name="line415"></a>  asyncTestCase.waitForAsync();
<a name="line416"></a>  var thenHasReturned = false;
<a name="line417"></a>  var p = goog.Promise.reject();
<a name="line418"></a>
<a name="line419"></a>  p.then(null, function() {
<a name="line420"></a>    assertTrue(thenHasReturned);
<a name="line421"></a>  });
<a name="line422"></a>  p.thenAlways(continueTesting);
<a name="line423"></a>  thenHasReturned = true;
<a name="line424"></a>}
<a name="line425"></a>
<a name="line426"></a>
<a name="line427"></a>function testResolutionOrder() {
<a name="line428"></a>  asyncTestCase.waitForAsync();
<a name="line429"></a>  var callbacks = [];
<a name="line430"></a>  var p = goog.Promise.resolve();
<a name="line431"></a>
<a name="line432"></a>  p.then(function() { callbacks.push(1); }, shouldNotCall);
<a name="line433"></a>  p.then(function() { callbacks.push(2); }, shouldNotCall);
<a name="line434"></a>  p.then(function() { callbacks.push(3); }, shouldNotCall);
<a name="line435"></a>
<a name="line436"></a>  p.then(function() {
<a name="line437"></a>    assertArrayEquals([1, 2, 3], callbacks);
<a name="line438"></a>  });
<a name="line439"></a>  p.thenAlways(continueTesting);
<a name="line440"></a>}
<a name="line441"></a>
<a name="line442"></a>
<a name="line443"></a>function testResolutionOrderWithThrow() {
<a name="line444"></a>  asyncTestCase.waitForAsync();
<a name="line445"></a>  var callbacks = [];
<a name="line446"></a>  var p = goog.Promise.resolve();
<a name="line447"></a>
<a name="line448"></a>  p.then(function() { callbacks.push(1); }, shouldNotCall);
<a name="line449"></a>  var child = p.then(function() {
<a name="line450"></a>    callbacks.push(2);
<a name="line451"></a>    throw Error();
<a name="line452"></a>  }, shouldNotCall);
<a name="line453"></a>
<a name="line454"></a>  child.then(shouldNotCall, function() {
<a name="line455"></a>    // The parent callbacks should be evaluated before the child.
<a name="line456"></a>    callbacks.push(4);
<a name="line457"></a>  });
<a name="line458"></a>
<a name="line459"></a>  p.then(function() { callbacks.push(3); }, shouldNotCall);
<a name="line460"></a>
<a name="line461"></a>  child.then(shouldNotCall, function() {
<a name="line462"></a>    callbacks.push(5);
<a name="line463"></a>    assertArrayEquals([1, 2, 3, 4, 5], callbacks);
<a name="line464"></a>  });
<a name="line465"></a>
<a name="line466"></a>  p.thenAlways(continueTesting);
<a name="line467"></a>}
<a name="line468"></a>
<a name="line469"></a>
<a name="line470"></a>function testResolutionOrderWithNestedThen() {
<a name="line471"></a>  asyncTestCase.waitForAsync();
<a name="line472"></a>  var callbacks = [];
<a name="line473"></a>  var p = goog.Promise.resolve();
<a name="line474"></a>
<a name="line475"></a>  p.then(function() {
<a name="line476"></a>    callbacks.push(1);
<a name="line477"></a>    p.then(function() {
<a name="line478"></a>      callbacks.push(3);
<a name="line479"></a>    });
<a name="line480"></a>  });
<a name="line481"></a>  p.then(function() { callbacks.push(2); });
<a name="line482"></a>
<a name="line483"></a>  window.setTimeout(function() {
<a name="line484"></a>    assertArrayEquals([1, 2, 3], callbacks);
<a name="line485"></a>    continueTesting();
<a name="line486"></a>  }, 100);
<a name="line487"></a>}
<a name="line488"></a>
<a name="line489"></a>
<a name="line490"></a>function testRejectionOrder() {
<a name="line491"></a>  asyncTestCase.waitForAsync();
<a name="line492"></a>  var callbacks = [];
<a name="line493"></a>  var p = goog.Promise.reject();
<a name="line494"></a>
<a name="line495"></a>  p.then(shouldNotCall, function() { callbacks.push(1); });
<a name="line496"></a>  p.then(shouldNotCall, function() { callbacks.push(2); });
<a name="line497"></a>  p.then(shouldNotCall, function() { callbacks.push(3); });
<a name="line498"></a>
<a name="line499"></a>  p.then(shouldNotCall, function() {
<a name="line500"></a>    assertArrayEquals([1, 2, 3], callbacks);
<a name="line501"></a>  });
<a name="line502"></a>  p.thenAlways(continueTesting);
<a name="line503"></a>}
<a name="line504"></a>
<a name="line505"></a>
<a name="line506"></a>function testRejectionOrderWithThrow() {
<a name="line507"></a>  asyncTestCase.waitForAsync();
<a name="line508"></a>  var callbacks = [];
<a name="line509"></a>  var p = goog.Promise.reject();
<a name="line510"></a>
<a name="line511"></a>  p.then(shouldNotCall, function() { callbacks.push(1); });
<a name="line512"></a>  p.then(shouldNotCall, function() {
<a name="line513"></a>    callbacks.push(2);
<a name="line514"></a>    throw Error();
<a name="line515"></a>  });
<a name="line516"></a>  p.then(shouldNotCall, function() { callbacks.push(3); });
<a name="line517"></a>
<a name="line518"></a>  p.then(shouldNotCall, function() {
<a name="line519"></a>    assertArrayEquals([1, 2, 3], callbacks);
<a name="line520"></a>  });
<a name="line521"></a>  p.thenAlways(continueTesting);
<a name="line522"></a>}
<a name="line523"></a>
<a name="line524"></a>
<a name="line525"></a>function testRejectionOrderWithNestedThen() {
<a name="line526"></a>  asyncTestCase.waitForAsync();
<a name="line527"></a>  var callbacks = [];
<a name="line528"></a>
<a name="line529"></a>  var p = goog.Promise.reject();
<a name="line530"></a>
<a name="line531"></a>  p.then(shouldNotCall, function() {
<a name="line532"></a>    callbacks.push(1);
<a name="line533"></a>    p.then(shouldNotCall, function() {
<a name="line534"></a>      callbacks.push(3);
<a name="line535"></a>    });
<a name="line536"></a>  });
<a name="line537"></a>  p.then(shouldNotCall, function() { callbacks.push(2); });
<a name="line538"></a>
<a name="line539"></a>  window.setTimeout(function() {
<a name="line540"></a>    assertArrayEquals([1, 2, 3], callbacks);
<a name="line541"></a>    continueTesting();
<a name="line542"></a>  }, 0);
<a name="line543"></a>}
<a name="line544"></a>
<a name="line545"></a>
<a name="line546"></a>function testBranching() {
<a name="line547"></a>  asyncTestCase.waitForSignals(3);
<a name="line548"></a>  var p = goog.Promise.resolve(2);
<a name="line549"></a>
<a name="line550"></a>  p.then(function(value) {
<a name="line551"></a>    assertEquals(&#39;then functions should see the same value&#39;, 2, value);
<a name="line552"></a>    return value / 2;
<a name="line553"></a>  }).then(function(value) {
<a name="line554"></a>    assertEquals(&#39;branch should receive the returned value&#39;, 1, value);
<a name="line555"></a>    asyncTestCase.signal();
<a name="line556"></a>  });
<a name="line557"></a>
<a name="line558"></a>  p.then(function(value) {
<a name="line559"></a>    assertEquals(&#39;then functions should see the same value&#39;, 2, value);
<a name="line560"></a>    throw value + 1;
<a name="line561"></a>  }).then(shouldNotCall, function(reason) {
<a name="line562"></a>    assertEquals(&#39;branch should receive the thrown value&#39;, 3, reason);
<a name="line563"></a>    asyncTestCase.signal();
<a name="line564"></a>  });
<a name="line565"></a>
<a name="line566"></a>  p.then(function(value) {
<a name="line567"></a>    assertEquals(&#39;then functions should see the same value&#39;, 2, value);
<a name="line568"></a>    return value * 2;
<a name="line569"></a>  }).then(function(value) {
<a name="line570"></a>    assertEquals(&#39;branch should receive the returned value&#39;, 4, value);
<a name="line571"></a>    asyncTestCase.signal();
<a name="line572"></a>  });
<a name="line573"></a>}
<a name="line574"></a>
<a name="line575"></a>
<a name="line576"></a>function testThenReturnsPromise() {
<a name="line577"></a>  var parent = goog.Promise.resolve();
<a name="line578"></a>  var child = parent.then();
<a name="line579"></a>
<a name="line580"></a>  assertTrue(child instanceof goog.Promise);
<a name="line581"></a>  assertNotEquals(&#39;The returned Promise must be different from the input.&#39;,
<a name="line582"></a>                  parent, child);
<a name="line583"></a>}
<a name="line584"></a>
<a name="line585"></a>
<a name="line586"></a>function testBlockingPromise() {
<a name="line587"></a>  asyncTestCase.waitForAsync();
<a name="line588"></a>  var p = goog.Promise.resolve();
<a name="line589"></a>  var wasFulfilled = false;
<a name="line590"></a>  var wasRejected = false;
<a name="line591"></a>
<a name="line592"></a>  var p2 = p.then(function() {
<a name="line593"></a>    return new goog.Promise(function(resolve, reject) {});
<a name="line594"></a>  });
<a name="line595"></a>
<a name="line596"></a>  p2.then(function() {
<a name="line597"></a>    wasFulfilled = true;
<a name="line598"></a>  }, function() {
<a name="line599"></a>    wasRejected = true;
<a name="line600"></a>  });
<a name="line601"></a>
<a name="line602"></a>  window.setTimeout(function() {
<a name="line603"></a>    assertFalse(&#39;p2 should be blocked on the returned Promise&#39;, wasFulfilled);
<a name="line604"></a>    assertFalse(&#39;p2 should be blocked on the returned Promise&#39;, wasRejected);
<a name="line605"></a>    continueTesting();
<a name="line606"></a>  }, 100);
<a name="line607"></a>}
<a name="line608"></a>
<a name="line609"></a>
<a name="line610"></a>function testBlockingPromiseFulfilled() {
<a name="line611"></a>  asyncTestCase.waitForAsync();
<a name="line612"></a>  var blockingPromise = new goog.Promise(function(resolve, reject) {
<a name="line613"></a>    window.setTimeout(function() {
<a name="line614"></a>      resolve(sentinel);
<a name="line615"></a>    }, 0);
<a name="line616"></a>  });
<a name="line617"></a>
<a name="line618"></a>  var p = goog.Promise.resolve(dummy);
<a name="line619"></a>  var p2 = p.then(function(value) {
<a name="line620"></a>    return blockingPromise;
<a name="line621"></a>  });
<a name="line622"></a>
<a name="line623"></a>  p2.then(function(value) {
<a name="line624"></a>    assertEquals(sentinel, value);
<a name="line625"></a>  }).thenAlways(continueTesting);
<a name="line626"></a>}
<a name="line627"></a>
<a name="line628"></a>
<a name="line629"></a>function testBlockingPromiseRejected() {
<a name="line630"></a>  asyncTestCase.waitForAsync();
<a name="line631"></a>  var blockingPromise = new goog.Promise(function(resolve, reject) {
<a name="line632"></a>    window.setTimeout(function() {
<a name="line633"></a>      reject(sentinel);
<a name="line634"></a>    }, 0);
<a name="line635"></a>  });
<a name="line636"></a>
<a name="line637"></a>  var p = goog.Promise.resolve(blockingPromise);
<a name="line638"></a>
<a name="line639"></a>  p.then(shouldNotCall, function(reason) {
<a name="line640"></a>    assertEquals(sentinel, reason);
<a name="line641"></a>  }).thenAlways(continueTesting);
<a name="line642"></a>}
<a name="line643"></a>
<a name="line644"></a>
<a name="line645"></a>function testBlockingThenableFulfilled() {
<a name="line646"></a>  asyncTestCase.waitForAsync();
<a name="line647"></a>  var thenable = {
<a name="line648"></a>    then: function(onFulfill, onReject) { onFulfill(sentinel); }
<a name="line649"></a>  };
<a name="line650"></a>
<a name="line651"></a>  var p = goog.Promise.resolve(thenable).
<a name="line652"></a>      then(function(reason) {
<a name="line653"></a>        assertEquals(sentinel, reason);
<a name="line654"></a>      }, shouldNotCall).thenAlways(continueTesting);
<a name="line655"></a>}
<a name="line656"></a>
<a name="line657"></a>
<a name="line658"></a>function testBlockingThenableRejected() {
<a name="line659"></a>  asyncTestCase.waitForAsync();
<a name="line660"></a>  var thenable = {
<a name="line661"></a>    then: function(onFulfill, onReject) { onReject(sentinel); }
<a name="line662"></a>  };
<a name="line663"></a>
<a name="line664"></a>  var p = goog.Promise.resolve(thenable).
<a name="line665"></a>      then(shouldNotCall, function(reason) {
<a name="line666"></a>        assertEquals(sentinel, reason);
<a name="line667"></a>      }).thenAlways(continueTesting);
<a name="line668"></a>}
<a name="line669"></a>
<a name="line670"></a>
<a name="line671"></a>function testBlockingThenableThrows() {
<a name="line672"></a>  asyncTestCase.waitForAsync();
<a name="line673"></a>  var thenable = {
<a name="line674"></a>    then: function(onFulfill, onReject) { throw sentinel; }
<a name="line675"></a>  };
<a name="line676"></a>
<a name="line677"></a>  var p = goog.Promise.resolve(thenable).
<a name="line678"></a>      then(shouldNotCall, function(reason) {
<a name="line679"></a>        assertEquals(sentinel, reason);
<a name="line680"></a>      }).thenAlways(continueTesting);
<a name="line681"></a>}
<a name="line682"></a>
<a name="line683"></a>
<a name="line684"></a>function testBlockingThenableMisbehaves() {
<a name="line685"></a>  asyncTestCase.waitForAsync();
<a name="line686"></a>  var thenable = {
<a name="line687"></a>    then: function(onFulfill, onReject) {
<a name="line688"></a>      onFulfill(sentinel);
<a name="line689"></a>      onFulfill(dummy);
<a name="line690"></a>      onReject(dummy);
<a name="line691"></a>      throw dummy;
<a name="line692"></a>    }
<a name="line693"></a>  };
<a name="line694"></a>
<a name="line695"></a>  var p = goog.Promise.resolve(thenable).
<a name="line696"></a>      then(function(value) {
<a name="line697"></a>        assertEquals(
<a name="line698"></a>            &#39;Only the first resolution of the Thenable should have a result.&#39;,
<a name="line699"></a>            sentinel, value);
<a name="line700"></a>      }, shouldNotCall).thenAlways(continueTesting);
<a name="line701"></a>}
<a name="line702"></a>
<a name="line703"></a>
<a name="line704"></a>function testNestingThenables() {
<a name="line705"></a>  asyncTestCase.waitForAsync();
<a name="line706"></a>  var thenableA = {
<a name="line707"></a>    then: function(onFulfill, onReject) { onFulfill(sentinel); }
<a name="line708"></a>  };
<a name="line709"></a>  var thenableB = {
<a name="line710"></a>    then: function(onFulfill, onReject) { onFulfill(thenableA); }
<a name="line711"></a>  };
<a name="line712"></a>  var thenableC = {
<a name="line713"></a>    then: function(onFulfill, onReject) { onFulfill(thenableB); }
<a name="line714"></a>  };
<a name="line715"></a>
<a name="line716"></a>  var p = goog.Promise.resolve(thenableC).
<a name="line717"></a>      then(function(value) {
<a name="line718"></a>        assertEquals(
<a name="line719"></a>            &#39;Should resolve to the fulfillment value of thenableA&#39;,
<a name="line720"></a>            sentinel, value);
<a name="line721"></a>      }, shouldNotCall).thenAlways(continueTesting);
<a name="line722"></a>}
<a name="line723"></a>
<a name="line724"></a>
<a name="line725"></a>function testNestingThenablesRejected() {
<a name="line726"></a>  asyncTestCase.waitForAsync();
<a name="line727"></a>  var thenableA = {
<a name="line728"></a>    then: function(onFulfill, onReject) { onReject(sentinel); }
<a name="line729"></a>  };
<a name="line730"></a>  var thenableB = {
<a name="line731"></a>    then: function(onFulfill, onReject) { onReject(thenableA); }
<a name="line732"></a>  };
<a name="line733"></a>  var thenableC = {
<a name="line734"></a>    then: function(onFulfill, onReject) { onReject(thenableB); }
<a name="line735"></a>  };
<a name="line736"></a>
<a name="line737"></a>  var p = goog.Promise.reject(thenableC).
<a name="line738"></a>      then(shouldNotCall, function(reason) {
<a name="line739"></a>        assertEquals(
<a name="line740"></a>            &#39;Should resolve to rejection reason of thenableA&#39;,
<a name="line741"></a>            sentinel, reason);
<a name="line742"></a>      }).thenAlways(continueTesting);
<a name="line743"></a>}
<a name="line744"></a>
<a name="line745"></a>
<a name="line746"></a>function testThenCatch() {
<a name="line747"></a>  asyncTestCase.waitForAsync();
<a name="line748"></a>  var catchCalled = false;
<a name="line749"></a>  var p = goog.Promise.reject();
<a name="line750"></a>
<a name="line751"></a>  var p2 = p.thenCatch(function(reason) {
<a name="line752"></a>    catchCalled = true;
<a name="line753"></a>    return sentinel;
<a name="line754"></a>  });
<a name="line755"></a>
<a name="line756"></a>  p2.then(function(value) {
<a name="line757"></a>    assertTrue(catchCalled);
<a name="line758"></a>    assertEquals(sentinel, value);
<a name="line759"></a>  }, shouldNotCall);
<a name="line760"></a>  p2.thenAlways(continueTesting);
<a name="line761"></a>}
<a name="line762"></a>
<a name="line763"></a>
<a name="line764"></a>function testRaceWithEmptyList() {
<a name="line765"></a>  asyncTestCase.waitForAsync();
<a name="line766"></a>  goog.Promise.race([]).then(function(value) {
<a name="line767"></a>    assertUndefined(value);
<a name="line768"></a>  }).thenAlways(continueTesting);
<a name="line769"></a>}
<a name="line770"></a>
<a name="line771"></a>
<a name="line772"></a>function testRaceWithFulfill() {
<a name="line773"></a>  asyncTestCase.waitForAsync();
<a name="line774"></a>
<a name="line775"></a>  var a = fulfillSoon(&#39;a&#39;, 40);
<a name="line776"></a>  var b = fulfillSoon(&#39;b&#39;, 30);
<a name="line777"></a>  var c = fulfillSoon(&#39;c&#39;, 10);
<a name="line778"></a>  var d = fulfillSoon(&#39;d&#39;, 20);
<a name="line779"></a>
<a name="line780"></a>  goog.Promise.race([a, b, c, d]).
<a name="line781"></a>      then(function(value) {
<a name="line782"></a>        assertEquals(&#39;c&#39;, value);
<a name="line783"></a>        // Return the slowest input promise to wait for it to complete.
<a name="line784"></a>        return a;
<a name="line785"></a>      }).
<a name="line786"></a>      then(function(value) {
<a name="line787"></a>        assertEquals(&#39;The slowest promise should resolve eventually.&#39;,
<a name="line788"></a>                     &#39;a&#39;, value);
<a name="line789"></a>      }).thenAlways(continueTesting);
<a name="line790"></a>}
<a name="line791"></a>
<a name="line792"></a>
<a name="line793"></a>function testRaceWithReject() {
<a name="line794"></a>  asyncTestCase.waitForAsync();
<a name="line795"></a>
<a name="line796"></a>  var a = rejectSoon(&#39;rejected-a&#39;, 40);
<a name="line797"></a>  var b = rejectSoon(&#39;rejected-b&#39;, 30);
<a name="line798"></a>  var c = rejectSoon(&#39;rejected-c&#39;, 10);
<a name="line799"></a>  var d = rejectSoon(&#39;rejected-d&#39;, 20);
<a name="line800"></a>
<a name="line801"></a>  var p = goog.Promise.race([a, b, c, d]).
<a name="line802"></a>      then(shouldNotCall, function(value) {
<a name="line803"></a>        assertEquals(&#39;rejected-c&#39;, value);
<a name="line804"></a>        return a;
<a name="line805"></a>      }).
<a name="line806"></a>      then(shouldNotCall, function(reason) {
<a name="line807"></a>        assertEquals(&#39;The slowest promise should resolve eventually.&#39;,
<a name="line808"></a>                     &#39;rejected-a&#39;, reason);
<a name="line809"></a>      }).thenAlways(continueTesting);
<a name="line810"></a>}
<a name="line811"></a>
<a name="line812"></a>
<a name="line813"></a>function testAllWithEmptyList() {
<a name="line814"></a>  asyncTestCase.waitForAsync();
<a name="line815"></a>  goog.Promise.all([]).then(function(value) {
<a name="line816"></a>    assertArrayEquals([], value);
<a name="line817"></a>  }).thenAlways(continueTesting);
<a name="line818"></a>}
<a name="line819"></a>
<a name="line820"></a>
<a name="line821"></a>function testAllWithFulfill() {
<a name="line822"></a>  asyncTestCase.waitForAsync();
<a name="line823"></a>
<a name="line824"></a>  var a = fulfillSoon(&#39;a&#39;, 40);
<a name="line825"></a>  var b = fulfillSoon(&#39;b&#39;, 30);
<a name="line826"></a>  var c = fulfillSoon(&#39;c&#39;, 10);
<a name="line827"></a>  var d = fulfillSoon(&#39;d&#39;, 20);
<a name="line828"></a>
<a name="line829"></a>  goog.Promise.all([a, b, c, d]).then(function(value) {
<a name="line830"></a>    assertArrayEquals([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], value);
<a name="line831"></a>  }).thenAlways(continueTesting);
<a name="line832"></a>}
<a name="line833"></a>
<a name="line834"></a>
<a name="line835"></a>function testAllWithReject() {
<a name="line836"></a>  asyncTestCase.waitForAsync();
<a name="line837"></a>
<a name="line838"></a>  var a = fulfillSoon(&#39;a&#39;, 40);
<a name="line839"></a>  var b = rejectSoon(&#39;rejected-b&#39;, 30);
<a name="line840"></a>  var c = fulfillSoon(&#39;c&#39;, 10);
<a name="line841"></a>  var d = fulfillSoon(&#39;d&#39;, 20);
<a name="line842"></a>
<a name="line843"></a>  goog.Promise.all([a, b, c, d]).
<a name="line844"></a>      then(shouldNotCall, function(reason) {
<a name="line845"></a>        assertEquals(&#39;rejected-b&#39;, reason);
<a name="line846"></a>        return a;
<a name="line847"></a>      }).
<a name="line848"></a>      then(function(value) {
<a name="line849"></a>        assertEquals(&#39;Promise &quot;a&quot; should be fulfilled even though the all()&#39; +
<a name="line850"></a>                     &#39;was rejected.&#39;, &#39;a&#39;, value);
<a name="line851"></a>      }).thenAlways(continueTesting);
<a name="line852"></a>}
<a name="line853"></a>
<a name="line854"></a>
<a name="line855"></a>function testFirstFulfilledWithEmptyList() {
<a name="line856"></a>  asyncTestCase.waitForAsync();
<a name="line857"></a>  goog.Promise.firstFulfilled([]).then(function(value) {
<a name="line858"></a>    assertUndefined(value);
<a name="line859"></a>  }).thenAlways(continueTesting);
<a name="line860"></a>}
<a name="line861"></a>
<a name="line862"></a>
<a name="line863"></a>function testFirstFulfilledWithFulfill() {
<a name="line864"></a>  asyncTestCase.waitForAsync();
<a name="line865"></a>
<a name="line866"></a>  var a = fulfillSoon(&#39;a&#39;, 40);
<a name="line867"></a>  var b = rejectSoon(&#39;rejected-b&#39;, 30);
<a name="line868"></a>  var c = rejectSoon(&#39;rejected-c&#39;, 10);
<a name="line869"></a>  var d = fulfillSoon(&#39;d&#39;, 20);
<a name="line870"></a>
<a name="line871"></a>  goog.Promise.firstFulfilled([a, b, c, d]).
<a name="line872"></a>      then(function(value) {
<a name="line873"></a>        assertEquals(&#39;d&#39;, value);
<a name="line874"></a>        return c;
<a name="line875"></a>      }).
<a name="line876"></a>      then(shouldNotCall, function(reason) {
<a name="line877"></a>        assertEquals(
<a name="line878"></a>            &#39;Promise &quot;c&quot; should have been rejected before the some() resolved.&#39;,
<a name="line879"></a>            &#39;rejected-c&#39;, reason);
<a name="line880"></a>        return a;
<a name="line881"></a>      }).
<a name="line882"></a>      then(function(reason) {
<a name="line883"></a>        assertEquals(
<a name="line884"></a>            &#39;Promise &quot;a&quot; should be fulfilled even after some() has resolved.&#39;,
<a name="line885"></a>            &#39;a&#39;, value);
<a name="line886"></a>      }, shouldNotCall).thenAlways(continueTesting);
<a name="line887"></a>}
<a name="line888"></a>
<a name="line889"></a>
<a name="line890"></a>function testFirstFulfilledWithReject() {
<a name="line891"></a>  asyncTestCase.waitForAsync();
<a name="line892"></a>
<a name="line893"></a>  var a = rejectSoon(&#39;rejected-a&#39;, 40);
<a name="line894"></a>  var b = rejectSoon(&#39;rejected-b&#39;, 30);
<a name="line895"></a>  var c = rejectSoon(&#39;rejected-c&#39;, 10);
<a name="line896"></a>  var d = rejectSoon(&#39;rejected-d&#39;, 20);
<a name="line897"></a>
<a name="line898"></a>  var p = goog.Promise.firstFulfilled([a, b, c, d]).
<a name="line899"></a>      then(shouldNotCall, function(reason) {
<a name="line900"></a>        assertArrayEquals(
<a name="line901"></a>            [&#39;rejected-a&#39;, &#39;rejected-b&#39;, &#39;rejected-c&#39;, &#39;rejected-d&#39;], reason);
<a name="line902"></a>      }).thenAlways(continueTesting);
<a name="line903"></a>}
<a name="line904"></a>
<a name="line905"></a>
<a name="line906"></a>function testThenAlwaysWithFulfill() {
<a name="line907"></a>  asyncTestCase.waitForAsync();
<a name="line908"></a>  var p = goog.Promise.resolve().
<a name="line909"></a>      thenAlways(function() {
<a name="line910"></a>        assertEquals(0, arguments.length);
<a name="line911"></a>      }).
<a name="line912"></a>      then(continueTesting, shouldNotCall);
<a name="line913"></a>}
<a name="line914"></a>
<a name="line915"></a>
<a name="line916"></a>function testThenAlwaysWithReject() {
<a name="line917"></a>  asyncTestCase.waitForAsync();
<a name="line918"></a>  var p = goog.Promise.reject().
<a name="line919"></a>      thenAlways(function() {
<a name="line920"></a>        assertEquals(0, arguments.length);
<a name="line921"></a>      }).
<a name="line922"></a>      then(shouldNotCall, continueTesting);
<a name="line923"></a>}
<a name="line924"></a>
<a name="line925"></a>
<a name="line926"></a>function testThenAlwaysCalledMultipleTimes() {
<a name="line927"></a>  asyncTestCase.waitForAsync();
<a name="line928"></a>  var calls = [];
<a name="line929"></a>
<a name="line930"></a>  var p = goog.Promise.resolve(sentinel);
<a name="line931"></a>  p.then(function(value) {
<a name="line932"></a>    assertEquals(sentinel, value);
<a name="line933"></a>    calls.push(1);
<a name="line934"></a>    return value;
<a name="line935"></a>  });
<a name="line936"></a>  p.thenAlways(function() {
<a name="line937"></a>    assertEquals(0, arguments.length);
<a name="line938"></a>    calls.push(2);
<a name="line939"></a>    throw Error(&#39;thenAlways throw&#39;);
<a name="line940"></a>  });
<a name="line941"></a>  p.then(function(value) {
<a name="line942"></a>    assertEquals(
<a name="line943"></a>        &#39;Promise result should not mutate after throw from thenAlways.&#39;,
<a name="line944"></a>        sentinel, value);
<a name="line945"></a>    calls.push(3);
<a name="line946"></a>  });
<a name="line947"></a>  p.thenAlways(function() {
<a name="line948"></a>    assertArrayEquals([1, 2, 3], calls);
<a name="line949"></a>  });
<a name="line950"></a>  p.thenAlways(function() {
<a name="line951"></a>    assertEquals(
<a name="line952"></a>        &#39;Should be one unhandled exception from the &quot;thenAlways throw&quot;.&#39;,
<a name="line953"></a>        1, unhandledRejections.getCallCount());
<a name="line954"></a>    var rejectionCall = unhandledRejections.popLastCall();
<a name="line955"></a>    assertEquals(1, rejectionCall.getArguments().length);
<a name="line956"></a>    var err = rejectionCall.getArguments()[0];
<a name="line957"></a>    assertEquals(&#39;thenAlways throw&#39;, err.message);
<a name="line958"></a>    assertEquals(goog.global, rejectionCall.getThis());
<a name="line959"></a>  });
<a name="line960"></a>  p.thenAlways(continueTesting);
<a name="line961"></a>}
<a name="line962"></a>
<a name="line963"></a>
<a name="line964"></a>function testContextWithInit() {
<a name="line965"></a>  var initContext;
<a name="line966"></a>  var p = new goog.Promise(function(resolve, reject) {
<a name="line967"></a>    initContext = this;
<a name="line968"></a>  }, sentinel);
<a name="line969"></a>  assertEquals(sentinel, initContext);
<a name="line970"></a>}
<a name="line971"></a>
<a name="line972"></a>
<a name="line973"></a>function testContextWithInitDefault() {
<a name="line974"></a>  var initContext;
<a name="line975"></a>  var p = new goog.Promise(function(resolve, reject) {
<a name="line976"></a>    initContext = this;
<a name="line977"></a>  });
<a name="line978"></a>  assertEquals(
<a name="line979"></a>      &#39;initFunc should default to being called in the global scope&#39;,
<a name="line980"></a>      goog.global, initContext);
<a name="line981"></a>}
<a name="line982"></a>
<a name="line983"></a>
<a name="line984"></a>function testContextWithFulfillment() {
<a name="line985"></a>  asyncTestCase.waitForAsync();
<a name="line986"></a>  var context = sentinel;
<a name="line987"></a>  var p = goog.Promise.resolve();
<a name="line988"></a>
<a name="line989"></a>  p.then(function() {
<a name="line990"></a>    assertEquals(
<a name="line991"></a>        &#39;Call should be made in the global scope if no context is specified.&#39;,
<a name="line992"></a>        goog.global, this);
<a name="line993"></a>  });
<a name="line994"></a>  p.then(function() {
<a name="line995"></a>    assertEquals(sentinel, this);
<a name="line996"></a>  }, shouldNotCall, sentinel);
<a name="line997"></a>  p.thenAlways(function() {
<a name="line998"></a>    assertEquals(sentinel, this);
<a name="line999"></a>    continueTesting();
<a name="line1000"></a>  }, sentinel);
<a name="line1001"></a>}
<a name="line1002"></a>
<a name="line1003"></a>
<a name="line1004"></a>function testContextWithRejection() {
<a name="line1005"></a>  asyncTestCase.waitForAsync();
<a name="line1006"></a>  var context = sentinel;
<a name="line1007"></a>  var p = goog.Promise.reject();
<a name="line1008"></a>
<a name="line1009"></a>  p.then(shouldNotCall, function() {
<a name="line1010"></a>    assertEquals(
<a name="line1011"></a>        &#39;Call should be made in the global scope if no context is specified.&#39;,
<a name="line1012"></a>        goog.global, this);
<a name="line1013"></a>  });
<a name="line1014"></a>  p.then(shouldNotCall, function() {
<a name="line1015"></a>    assertEquals(sentinel, this);
<a name="line1016"></a>  }, sentinel);
<a name="line1017"></a>  p.thenCatch(function() {
<a name="line1018"></a>    assertEquals(sentinel, this);
<a name="line1019"></a>  }, sentinel);
<a name="line1020"></a>  p.thenAlways(function() {
<a name="line1021"></a>    assertEquals(sentinel, this);
<a name="line1022"></a>    continueTesting();
<a name="line1023"></a>  }, sentinel);
<a name="line1024"></a>}
<a name="line1025"></a>
<a name="line1026"></a>
<a name="line1027"></a>function testCancel() {
<a name="line1028"></a>  asyncTestCase.waitForAsync();
<a name="line1029"></a>  var p = new goog.Promise(goog.nullFunction);
<a name="line1030"></a>  p.then(shouldNotCall, function(reason) {
<a name="line1031"></a>    assertTrue(reason instanceof goog.Promise.CancellationError);
<a name="line1032"></a>    assertEquals(&#39;cancellation message&#39;, reason.message);
<a name="line1033"></a>    continueTesting();
<a name="line1034"></a>  });
<a name="line1035"></a>  p.cancel(&#39;cancellation message&#39;);
<a name="line1036"></a>}
<a name="line1037"></a>
<a name="line1038"></a>
<a name="line1039"></a>function testCancelAfterResolve() {
<a name="line1040"></a>  asyncTestCase.waitForAsync();
<a name="line1041"></a>  var p = goog.Promise.resolve();
<a name="line1042"></a>  p.cancel();
<a name="line1043"></a>  p.then(null, shouldNotCall);
<a name="line1044"></a>  p.thenAlways(continueTesting);
<a name="line1045"></a>}
<a name="line1046"></a>
<a name="line1047"></a>
<a name="line1048"></a>function testCancelAfterReject() {
<a name="line1049"></a>  asyncTestCase.waitForAsync();
<a name="line1050"></a>  var p = goog.Promise.reject(sentinel);
<a name="line1051"></a>  p.cancel();
<a name="line1052"></a>  p.then(shouldNotCall, function(reason) {
<a name="line1053"></a>    assertEquals(sentinel, reason);
<a name="line1054"></a>    continueTesting();
<a name="line1055"></a>  });
<a name="line1056"></a>}
<a name="line1057"></a>
<a name="line1058"></a>
<a name="line1059"></a>function testCancelPropagation() {
<a name="line1060"></a>  asyncTestCase.waitForSignals(2);
<a name="line1061"></a>  var cancelError;
<a name="line1062"></a>  var p = new goog.Promise(goog.nullFunction);
<a name="line1063"></a>
<a name="line1064"></a>  var p2 = p.then(shouldNotCall, function(reason) {
<a name="line1065"></a>    cancelError = reason;
<a name="line1066"></a>    assertTrue(reason instanceof goog.Promise.CancellationError);
<a name="line1067"></a>    assertEquals(&#39;parent cancel message&#39;, reason.message);
<a name="line1068"></a>    return sentinel;
<a name="line1069"></a>  });
<a name="line1070"></a>  p2.then(function(value) {
<a name="line1071"></a>    assertEquals(
<a name="line1072"></a>        &#39;Child promises should receive the returned value of the parent.&#39;,
<a name="line1073"></a>        sentinel, value);
<a name="line1074"></a>    asyncTestCase.signal();
<a name="line1075"></a>  }, shouldNotCall);
<a name="line1076"></a>
<a name="line1077"></a>  var p3 = p.then(shouldNotCall, function(reason) {
<a name="line1078"></a>    assertEquals(
<a name="line1079"></a>        &#39;Every onRejected handler should receive the same cancel error.&#39;,
<a name="line1080"></a>        cancelError, reason);
<a name="line1081"></a>    assertEquals(&#39;parent cancel message&#39;, reason.message);
<a name="line1082"></a>    asyncTestCase.signal();
<a name="line1083"></a>  });
<a name="line1084"></a>
<a name="line1085"></a>  p.cancel(&#39;parent cancel message&#39;);
<a name="line1086"></a>}
<a name="line1087"></a>
<a name="line1088"></a>
<a name="line1089"></a>function testCancelPropagationUpward() {
<a name="line1090"></a>  asyncTestCase.waitForAsync();
<a name="line1091"></a>  var cancelError;
<a name="line1092"></a>  var cancelCalls = [];
<a name="line1093"></a>  var parent = new goog.Promise(goog.nullFunction);
<a name="line1094"></a>
<a name="line1095"></a>  var child = parent.then(shouldNotCall, function(reason) {
<a name="line1096"></a>    assertTrue(reason instanceof goog.Promise.CancellationError);
<a name="line1097"></a>    assertEquals(&#39;grandChild cancel message&#39;, reason.message);
<a name="line1098"></a>    cancelError = reason;
<a name="line1099"></a>    cancelCalls.push(&#39;parent&#39;);
<a name="line1100"></a>  });
<a name="line1101"></a>
<a name="line1102"></a>  var grandChild = child.then(shouldNotCall, function(reason) {
<a name="line1103"></a>    assertEquals(&#39;Child should receive the same cancel error.&#39;,
<a name="line1104"></a>                 cancelError, reason);
<a name="line1105"></a>    cancelCalls.push(&#39;child&#39;);
<a name="line1106"></a>  });
<a name="line1107"></a>
<a name="line1108"></a>  grandChild.then(shouldNotCall, function(reason) {
<a name="line1109"></a>    assertEquals(&#39;GrandChild should receive the same cancel error.&#39;,
<a name="line1110"></a>                 cancelError, reason);
<a name="line1111"></a>    cancelCalls.push(&#39;grandChild&#39;);
<a name="line1112"></a>  });
<a name="line1113"></a>
<a name="line1114"></a>  grandChild.then(shouldNotCall, function(reason) {
<a name="line1115"></a>    assertArrayEquals(
<a name="line1116"></a>        &#39;Each promise in the hierarchy has a single child, so canceling the &#39; +
<a name="line1117"></a>        &#39;grandChild should cancel each ancestor in order.&#39;,
<a name="line1118"></a>        [&#39;parent&#39;, &#39;child&#39;, &#39;grandChild&#39;], cancelCalls);
<a name="line1119"></a>  }).thenAlways(continueTesting);
<a name="line1120"></a>
<a name="line1121"></a>  grandChild.cancel(&#39;grandChild cancel message&#39;);
<a name="line1122"></a>}
<a name="line1123"></a>
<a name="line1124"></a>
<a name="line1125"></a>function testCancelPropagationUpwardWithMultipleChildren() {
<a name="line1126"></a>  asyncTestCase.waitForAsync();
<a name="line1127"></a>  var cancelError;
<a name="line1128"></a>  var cancelCalls = [];
<a name="line1129"></a>  var parent = fulfillSoon(sentinel, 0);
<a name="line1130"></a>
<a name="line1131"></a>  parent.then(function(value) {
<a name="line1132"></a>    assertEquals(
<a name="line1133"></a>        &#39;Non-canceled callbacks should be called after a sibling is canceled.&#39;,
<a name="line1134"></a>        sentinel, value);
<a name="line1135"></a>    continueTesting();
<a name="line1136"></a>  });
<a name="line1137"></a>
<a name="line1138"></a>  var child = parent.then(shouldNotCall, function(reason) {
<a name="line1139"></a>    assertTrue(reason instanceof goog.Promise.CancellationError);
<a name="line1140"></a>    assertEquals(&#39;grandChild cancel message&#39;, reason.message);
<a name="line1141"></a>    cancelError = reason;
<a name="line1142"></a>    cancelCalls.push(&#39;child&#39;);
<a name="line1143"></a>  });
<a name="line1144"></a>
<a name="line1145"></a>  var grandChild = child.then(shouldNotCall, function(reason) {
<a name="line1146"></a>    assertEquals(reason, cancelError);
<a name="line1147"></a>    cancelCalls.push(&#39;grandChild&#39;);
<a name="line1148"></a>  });
<a name="line1149"></a>
<a name="line1150"></a>  grandChild.then(shouldNotCall, function(reason) {
<a name="line1151"></a>    assertEquals(reason, cancelError);
<a name="line1152"></a>    assertArrayEquals(
<a name="line1153"></a>        &#39;The parent promise has multiple children, so only the child and &#39; +
<a name="line1154"></a>        &#39;grandChild should be canceled.&#39;,
<a name="line1155"></a>        [&#39;child&#39;, &#39;grandChild&#39;], cancelCalls);
<a name="line1156"></a>  });
<a name="line1157"></a>
<a name="line1158"></a>  grandChild.cancel(&#39;grandChild cancel message&#39;);
<a name="line1159"></a>}
<a name="line1160"></a>
<a name="line1161"></a>
<a name="line1162"></a>function testCancelRecovery() {
<a name="line1163"></a>  asyncTestCase.waitForSignals(2);
<a name="line1164"></a>  var cancelError;
<a name="line1165"></a>  var cancelCalls = [];
<a name="line1166"></a>
<a name="line1167"></a>  var parent = fulfillSoon(sentinel, 100);
<a name="line1168"></a>
<a name="line1169"></a>  var sibling1 = parent.then(function(value) {
<a name="line1170"></a>    assertEquals(
<a name="line1171"></a>        &#39;Non-canceled callbacks should be called after a sibling is canceled.&#39;,
<a name="line1172"></a>        sentinel, value);
<a name="line1173"></a>  });
<a name="line1174"></a>
<a name="line1175"></a>  var sibling2 = parent.then(shouldNotCall, function(reason) {
<a name="line1176"></a>    assertTrue(reason instanceof goog.Promise.CancellationError);
<a name="line1177"></a>    cancelError = reason;
<a name="line1178"></a>    cancelCalls.push(&#39;sibling2&#39;);
<a name="line1179"></a>    return sentinel;
<a name="line1180"></a>  });
<a name="line1181"></a>
<a name="line1182"></a>  parent.thenAlways(function() {
<a name="line1183"></a>    asyncTestCase.signal();
<a name="line1184"></a>  });
<a name="line1185"></a>
<a name="line1186"></a>  var grandChild = sibling2.then(function(value) {
<a name="line1187"></a>    cancelCalls.push(&#39;child&#39;);
<a name="line1188"></a>    assertEquals(
<a name="line1189"></a>        &#39;Returning a non-cancel value should uncancel the grandChild.&#39;,
<a name="line1190"></a>        value, sentinel);
<a name="line1191"></a>    assertArrayEquals([&#39;sibling2&#39;, &#39;child&#39;], cancelCalls);
<a name="line1192"></a>  }, shouldNotCall).thenAlways(function() {
<a name="line1193"></a>    asyncTestCase.signal();
<a name="line1194"></a>  });
<a name="line1195"></a>
<a name="line1196"></a>  grandChild.cancel();
<a name="line1197"></a>}
<a name="line1198"></a>
<a name="line1199"></a>
<a name="line1200"></a>function testCancellationError() {
<a name="line1201"></a>  var err = new goog.Promise.CancellationError(&#39;cancel message&#39;);
<a name="line1202"></a>  assertTrue(err instanceof Error);
<a name="line1203"></a>  assertTrue(err instanceof goog.Promise.CancellationError);
<a name="line1204"></a>  assertEquals(&#39;cancel&#39;, err.name);
<a name="line1205"></a>  assertEquals(&#39;cancel message&#39;, err.message);
<a name="line1206"></a>}
<a name="line1207"></a>
<a name="line1208"></a>
<a name="line1209"></a>function testMockClock() {
<a name="line1210"></a>  mockClock.install();
<a name="line1211"></a>
<a name="line1212"></a>  var resolveA;
<a name="line1213"></a>  var resolveB;
<a name="line1214"></a>  var calls = [];
<a name="line1215"></a>
<a name="line1216"></a>  var p = new goog.Promise(function(resolve, reject) {
<a name="line1217"></a>    resolveA = resolve;
<a name="line1218"></a>  });
<a name="line1219"></a>
<a name="line1220"></a>  p.then(function(value) {
<a name="line1221"></a>    assertEquals(sentinel, value);
<a name="line1222"></a>    calls.push(&#39;then&#39;);
<a name="line1223"></a>  });
<a name="line1224"></a>
<a name="line1225"></a>  var fulfilledChild = p.then(function(value) {
<a name="line1226"></a>    assertEquals(sentinel, value);
<a name="line1227"></a>    return goog.Promise.resolve(1);
<a name="line1228"></a>  }).then(function(value) {
<a name="line1229"></a>    assertEquals(1, value);
<a name="line1230"></a>    calls.push(&#39;fulfilledChild&#39;);
<a name="line1231"></a>
<a name="line1232"></a>  });
<a name="line1233"></a>
<a name="line1234"></a>  var rejectedChild = p.then(function(value) {
<a name="line1235"></a>    assertEquals(sentinel, value);
<a name="line1236"></a>    return goog.Promise.reject(2);
<a name="line1237"></a>  }).then(shouldNotCall, function(reason) {
<a name="line1238"></a>    assertEquals(2, reason);
<a name="line1239"></a>    calls.push(&#39;rejectedChild&#39;);
<a name="line1240"></a>  });
<a name="line1241"></a>
<a name="line1242"></a>  var unresolvedChild = p.then(function(value) {
<a name="line1243"></a>    assertEquals(sentinel, value);
<a name="line1244"></a>    return new goog.Promise(function(r) {
<a name="line1245"></a>      resolveB = r;
<a name="line1246"></a>    });
<a name="line1247"></a>  }).then(function(value) {
<a name="line1248"></a>    assertEquals(3, value);
<a name="line1249"></a>    calls.push(&#39;unresolvedChild&#39;);
<a name="line1250"></a>  });
<a name="line1251"></a>
<a name="line1252"></a>  resolveA(sentinel);
<a name="line1253"></a>  assertArrayEquals(
<a name="line1254"></a>      &#39;Calls must not be resolved until the clock ticks.&#39;,
<a name="line1255"></a>      [], calls);
<a name="line1256"></a>
<a name="line1257"></a>  mockClock.tick();
<a name="line1258"></a>  assertArrayEquals(
<a name="line1259"></a>      &#39;All resolved Promises should execute in the same timestep.&#39;,
<a name="line1260"></a>      [&#39;then&#39;, &#39;fulfilledChild&#39;, &#39;rejectedChild&#39;], calls);
<a name="line1261"></a>
<a name="line1262"></a>  resolveB(3);
<a name="line1263"></a>  assertArrayEquals(
<a name="line1264"></a>      &#39;New calls must not resolve until the clock ticks.&#39;,
<a name="line1265"></a>      [&#39;then&#39;, &#39;fulfilledChild&#39;, &#39;rejectedChild&#39;], calls);
<a name="line1266"></a>
<a name="line1267"></a>  mockClock.tick();
<a name="line1268"></a>  assertArrayEquals(
<a name="line1269"></a>      &#39;All callbacks should have executed.&#39;,
<a name="line1270"></a>      [&#39;then&#39;, &#39;fulfilledChild&#39;, &#39;rejectedChild&#39;, &#39;unresolvedChild&#39;], calls);
<a name="line1271"></a>}
<a name="line1272"></a>
<a name="line1273"></a>
<a name="line1274"></a>function testHandledRejection() {
<a name="line1275"></a>  mockClock.install();
<a name="line1276"></a>  goog.Promise.reject(sentinel).then(shouldNotCall, function(reason) {});
<a name="line1277"></a>
<a name="line1278"></a>  mockClock.tick();
<a name="line1279"></a>  assertEquals(0, unhandledRejections.getCallCount());
<a name="line1280"></a>}
<a name="line1281"></a>
<a name="line1282"></a>
<a name="line1283"></a>function testUnhandledRejection() {
<a name="line1284"></a>  mockClock.install();
<a name="line1285"></a>  goog.Promise.reject(sentinel);
<a name="line1286"></a>
<a name="line1287"></a>  mockClock.tick();
<a name="line1288"></a>  assertEquals(1, unhandledRejections.getCallCount());
<a name="line1289"></a>  var rejectionCall = unhandledRejections.popLastCall();
<a name="line1290"></a>  assertArrayEquals([sentinel], rejectionCall.getArguments());
<a name="line1291"></a>  assertEquals(goog.global, rejectionCall.getThis());
<a name="line1292"></a>}
<a name="line1293"></a>
<a name="line1294"></a>
<a name="line1295"></a>function testUnhandledBlockingRejection() {
<a name="line1296"></a>  mockClock.install();
<a name="line1297"></a>  var blocker = goog.Promise.reject(sentinel);
<a name="line1298"></a>  goog.Promise.resolve(blocker);
<a name="line1299"></a>
<a name="line1300"></a>  mockClock.tick();
<a name="line1301"></a>  assertEquals(1, unhandledRejections.getCallCount());
<a name="line1302"></a>  var rejectionCall = unhandledRejections.popLastCall();
<a name="line1303"></a>  assertArrayEquals([sentinel], rejectionCall.getArguments());
<a name="line1304"></a>  assertEquals(goog.global, rejectionCall.getThis());
<a name="line1305"></a>}
<a name="line1306"></a>
<a name="line1307"></a>
<a name="line1308"></a>function testHandledBlockingRejection() {
<a name="line1309"></a>  mockClock.install();
<a name="line1310"></a>  var blocker = goog.Promise.reject(sentinel);
<a name="line1311"></a>  goog.Promise.resolve(blocker).then(shouldNotCall, function(reason) {});
<a name="line1312"></a>
<a name="line1313"></a>  mockClock.tick();
<a name="line1314"></a>  assertEquals(0, unhandledRejections.getCallCount());
<a name="line1315"></a>}
<a name="line1316"></a>
<a name="line1317"></a>
<a name="line1318"></a>function testUnhandledRejectionWithTimeout() {
<a name="line1319"></a>  mockClock.install();
<a name="line1320"></a>  stubs.replace(goog.Promise, &#39;UNHANDLED_REJECTION_DELAY&#39;, 200);
<a name="line1321"></a>  goog.Promise.reject(sentinel);
<a name="line1322"></a>
<a name="line1323"></a>  mockClock.tick(199);
<a name="line1324"></a>  assertEquals(0, unhandledRejections.getCallCount());
<a name="line1325"></a>
<a name="line1326"></a>  mockClock.tick(1);
<a name="line1327"></a>  assertEquals(1, unhandledRejections.getCallCount());
<a name="line1328"></a>}
<a name="line1329"></a>
<a name="line1330"></a>
<a name="line1331"></a>function testHandledRejectionWithTimeout() {
<a name="line1332"></a>  mockClock.install();
<a name="line1333"></a>  stubs.replace(goog.Promise, &#39;UNHANDLED_REJECTION_DELAY&#39;, 200);
<a name="line1334"></a>  var p = goog.Promise.reject(sentinel);
<a name="line1335"></a>
<a name="line1336"></a>  mockClock.tick(199);
<a name="line1337"></a>  p.then(shouldNotCall, function(reason) {});
<a name="line1338"></a>
<a name="line1339"></a>  mockClock.tick(1);
<a name="line1340"></a>  assertEquals(0, unhandledRejections.getCallCount());
<a name="line1341"></a>}
<a name="line1342"></a>
<a name="line1343"></a>
<a name="line1344"></a>function testUnhandledRejectionDisabled() {
<a name="line1345"></a>  mockClock.install();
<a name="line1346"></a>  stubs.replace(goog.Promise, &#39;UNHANDLED_REJECTION_DELAY&#39;, -1);
<a name="line1347"></a>  goog.Promise.reject(sentinel);
<a name="line1348"></a>
<a name="line1349"></a>  mockClock.tick();
<a name="line1350"></a>  assertEquals(0, unhandledRejections.getCallCount());
<a name="line1351"></a>}
<a name="line1352"></a>
<a name="line1353"></a>
<a name="line1354"></a>function testThenableInterface() {
<a name="line1355"></a>  var promise = new goog.Promise(function(resolve, reject) {});
<a name="line1356"></a>  assertTrue(goog.Thenable.isImplementedBy(promise));
<a name="line1357"></a>
<a name="line1358"></a>  assertFalse(goog.Thenable.isImplementedBy({}));
<a name="line1359"></a>  assertFalse(goog.Thenable.isImplementedBy(&#39;string&#39;));
<a name="line1360"></a>  assertFalse(goog.Thenable.isImplementedBy(1));
<a name="line1361"></a>  assertFalse(goog.Thenable.isImplementedBy({then: function() {}}));
<a name="line1362"></a>
<a name="line1363"></a>  function T() {}
<a name="line1364"></a>  T.prototype.then = function(opt_a, opt_b, opt_c) {};
<a name="line1365"></a>  goog.Thenable.addImplementation(T);
<a name="line1366"></a>  assertTrue(goog.Thenable.isImplementedBy(new T));
<a name="line1367"></a>
<a name="line1368"></a>  // Test COMPILED code path.
<a name="line1369"></a>  try {
<a name="line1370"></a>    COMPIlED = true;
<a name="line1371"></a>    function C() {}
<a name="line1372"></a>    C.prototype.then = function(opt_a, opt_b, opt_c) {};
<a name="line1373"></a>    goog.Thenable.addImplementation(C);
<a name="line1374"></a>    assertTrue(goog.Thenable.isImplementedBy(new C));
<a name="line1375"></a>  } finally {
<a name="line1376"></a>    COMPILED = false;
<a name="line1377"></a>  }
<a name="line1378"></a>}
<a name="line1379"></a>
<a name="line1380"></a>
<a name="line1381"></a>function testCreateWithResolver_Resolved() {
<a name="line1382"></a>  mockClock.install();
<a name="line1383"></a>  var timesCalled = 0;
<a name="line1384"></a>
<a name="line1385"></a>  var resolver = goog.Promise.withResolver();
<a name="line1386"></a>
<a name="line1387"></a>  resolver.promise.then(function(value) {
<a name="line1388"></a>    timesCalled++;
<a name="line1389"></a>    assertEquals(sentinel, value);
<a name="line1390"></a>  }, fail);
<a name="line1391"></a>
<a name="line1392"></a>  assertEquals(&#39;then() must return before callbacks are invoked.&#39;,
<a name="line1393"></a>      0, timesCalled);
<a name="line1394"></a>
<a name="line1395"></a>  mockClock.tick();
<a name="line1396"></a>
<a name="line1397"></a>  assertEquals(&#39;promise is not resolved until resolver is invoked.&#39;,
<a name="line1398"></a>      0, timesCalled);
<a name="line1399"></a>
<a name="line1400"></a>  resolver.resolve(sentinel);
<a name="line1401"></a>
<a name="line1402"></a>  assertEquals(&#39;resolution is delayed until the next tick&#39;,
<a name="line1403"></a>      0, timesCalled);
<a name="line1404"></a>
<a name="line1405"></a>  mockClock.tick();
<a name="line1406"></a>
<a name="line1407"></a>  assertEquals(&#39;onFulfilled must be called exactly once.&#39;, 1, timesCalled);
<a name="line1408"></a>}
<a name="line1409"></a>
<a name="line1410"></a>
<a name="line1411"></a>function testCreateWithResolver_Rejected() {
<a name="line1412"></a>  mockClock.install();
<a name="line1413"></a>  var timesCalled = 0;
<a name="line1414"></a>
<a name="line1415"></a>  var resolver = goog.Promise.withResolver();
<a name="line1416"></a>
<a name="line1417"></a>  resolver.promise.then(fail, function(reason) {
<a name="line1418"></a>    timesCalled++;
<a name="line1419"></a>    assertEquals(sentinel, reason);
<a name="line1420"></a>  });
<a name="line1421"></a>
<a name="line1422"></a>  assertEquals(&#39;then() must return before callbacks are invoked.&#39;,
<a name="line1423"></a>      0, timesCalled);
<a name="line1424"></a>
<a name="line1425"></a>  mockClock.tick();
<a name="line1426"></a>
<a name="line1427"></a>  assertEquals(&#39;promise is not resolved until resolver is invoked.&#39;,
<a name="line1428"></a>      0, timesCalled);
<a name="line1429"></a>
<a name="line1430"></a>  resolver.reject(sentinel);
<a name="line1431"></a>
<a name="line1432"></a>  assertEquals(&#39;resolution is delayed until the next tick&#39;,
<a name="line1433"></a>      0, timesCalled);
<a name="line1434"></a>
<a name="line1435"></a>  mockClock.tick();
<a name="line1436"></a>
<a name="line1437"></a>  assertEquals(&#39;onFulfilled must be called exactly once.&#39;, 1, timesCalled);
<a name="line1438"></a>}
</pre>


</body>
</html>
